package ModelLayer;
import java.util.*;
import java.util.Iterator;

/**
 * Write a description of class LoanCollection here.
 * 
 * @author (Group2) 
 * @version (16)
 */
public class LoanCollection
{
    private static final LoanCollection instance = new LoanCollection();        //singleton
    
    public ArrayList<Loan> loancollection;      //instance variables;
    
    public LoanCollection()     //constructor of class LoanCollection;
    {
        loancollection=new ArrayList();
    }
    
    public static LoanCollection getInstance()      //singleton of the class
    {
       return instance;
    }
        
    public void addLoan(Loan loan)      //Adds a loan to the Collection;
    {
        loancollection.add(loan);
    }
    
    public void readLoan(String personId)       //returns the information about the loan;
    {
        for(Loan a: loancollection)
        {
            if(a.personID.equals(personId))
            {
                a.print();
            }
        }
    }
    
       
    
    public void deleteLoan(String personId)     //deletes the loan in the case if something is wrong;
    {
        
        Iterator<Loan> it = loancollection.iterator();
        while(it.hasNext())
        {
            if(it.next().personID.equals(personId))
            {
                it.remove();
            }
        }
    }
    
    
    
    
    
    public void returnLoan(String loanId)       //if someone has returned the loan, this method handles it;
    {
        Iterator<Loan> it = loancollection.iterator();
        while(it.hasNext())
        {
            if(it.next().id.equals(loanId))
            {
                it.remove();
            }
        }
    }
    
    public void updateLoan(String personId, String borrowDate, String period, String copyID)    //updates the information about the loan;
    {
        for(Loan a: loancollection)
        {
            if(a.personID.equals(personId))
            {
                a.setBorrowDate(borrowDate);
                a.setPeriod(period);
                a.setCopyID(copyID);
            }
        }
    }
}
